package standard;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import server.GameHostProvider;
import server.MatchMakingServer;
import util.Logger;

public class ServerGUI extends JFrame {

	GameHostProvider provider;
	MatchMakingServer matchMaker;
	JTextArea output = new JTextArea();

	public ServerGUI() throws IOException {
		super("Shooter Server");
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				shutdown();
			}
		});
		Logger.lockOutputTo(output);
		output.setWrapStyleWord(true);
		output.setLineWrap(true);
		output.setEditable(false);
		setContentPane(new JScrollPane(output));
		setSize(600, 300);
		setLocationRelativeTo(null);
		setVisible(true);
		provider = new ShooterHostProvider();
		matchMaker = new MatchMakingServer(provider);
	}

	private void shutdown() {
		if (matchMaker != null)
			matchMaker.exit();
		System.exit(0);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					new ServerGUI();
				} catch (Exception e) {
					Logger.log(e);
				}
			}
		});
	}

}
